<?php
/**
 * Created by PhpStorm.
 * User: wwz894326653
 * Date: 2020/6/22
 * Time: 11:22
 */

namespace frontend\controllers;


use common\models\Help;
use common\models\Member;
use common\models\Product;
use common\models\ServiceRecycle;
use common\models\Banner;
use common\models\Category;
use frontend\models\ProductSearch;
use yii\rest\Controller;

class BannerController extends Controller
{
    /*
     * 轮播图
     */
    public function actionGetBanner()
    {
       return Banner::find()->all();
    }

    /*
     * 顶级分类
     */
    public function actionCategory()
    {
        return Category::find()->where(['pid'=>0])->all();
    }

   /*
    * 品牌
    */
    public function actionBrand($id)
    {
        return Category::find()->where(['pid'=>$id])->all();
    }

    /*
     * 商品
     */
    public function actionProduct($id)
    {
        return Product::find()->where(['category_id'=>$id])->all();
    }

    /*
     * 帮助中心
     */
    public function actionHelp()
    {
       return Help::find()->select('id,title')->all();
    }

    /*
     *帮助详情
     */
    public function actionHelpDetails($id)
    {
        return Help::find()->where("id=$id")->all();
    }

    /*
     * 服务条款&回收流程
     */
    public function actionServiceRecycle()
    {
       return ServiceRecycle::find()->all();
    }


    /*
     * 注册
     */
    public function actionRegister()
    {
        try {
            $model=new Member();
            $data=\Yii::$app->request->post();
            $model->username=$data['username'];
            $model->password=\Yii::$app->security->generatePasswordHash($data['password']);
            $model->created_at=time();
            $model->updated_at=time();
            if(!$model->save()){
                throw new \Exception('注册异常');
            }
            return $model;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }

    }

    /*
     * 登陆
     */
    public function actionLogin()
    {
        $data=\Yii::$app->request->post();
        /* @var  $model Member*/
        $model=Member::find()->where(['username'=>$data['username']])->one();
       if(!$model){
           throw new \Exception('用户名或密码错误');
       }
        if (!\Yii::$app->security->validatePassword($data['password'], $model->password)) {
            throw new \Exception("用户名或密码错误！");
        }
        return [
            "access_token" => $model->generateAccessToken(),
            "userInfo"     => $model
        ];
    }

    /*
     * 热门商品
     */
    public function actionHot()
    {
        return Product::find()->where(['is_hot'=>1])->all();
    }

    /*
     * 热门品牌
     */
    public function actionHotBrand()
    {
        return Category::find()->andWhere(['<>','pid', 0])->andWhere(['is_hot'=>1])->all();
    }

    /*
     * 搜索
     */
    public function actionSearch()
    {
//        $data=\Yii::$app->request->get('name');
//         return   Product::find()->where(['like','name',$data])->all();
        $searModel= new ProductSearch();
        $data=$searModel->search(\Yii::$app->request->queryParams);
        $res=$data->getModels();
        var_dump($res);

    }

    /*
     * 热门搜索
     */
    public function actionSearchHot()
    {
        return Product::find()->where(['is_search'=>1])->all();
    }
}